package dt.inlee.system.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjb.common.util.MD5;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysUser;
import dt.inlee.system.service.SysUserService;
import dt.inlee.system.vo.SysUserLoginBean;
import dt.inlee.system.vo.UserPassWordBean;

public class UserInfoPwdAction extends ActionSupport {
	
	/** 
    * @Fields serialVersionUID : TODO() 
    */ 
    private static final long serialVersionUID = -4206161704574443354L;

    //用户业务层
	@Autowired
	private SysUserService sysUserService;
	
	//返回JSON对象
	private JSONObject jsonObject = new JSONObject();
	

	public JSONObject getJsonObject() {
		return jsonObject;
	}


	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	private UserPassWordBean userPassWordBean = new UserPassWordBean();

	public UserPassWordBean getUserPassWordBean() {
		return userPassWordBean;
	}


	public void setUserPassWordBean(UserPassWordBean userPassWordBean) {
		this.userPassWordBean = userPassWordBean;
	}

	
	/**
	 * 获取用户名
	 * @return
	 */
	@Action(value="showUserName",results={ @Result(type = "json", name = "success", params = {
			"root", "jsonObject" }) })
	public String showUserName(){
		// 当前登录用户
		SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext
		.getContext().getSession().get(CommonConstant.SESSION_USER);
		SysUser loginUser = null;
		try {
			loginUser = this.sysUserService.findSysUserById(sysUserLoginBean.getId());
		} catch (FrameworkException e) {
			e.printStackTrace();
		}
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String username  = loginUser.getUserName().split("@")[0];
		jsonMap.put("username", username);
		
		jsonObject = JSONObject.fromObject(jsonMap);
		
		return SUCCESS;
		
	}
	/**
	 * 密码修改
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws FrameworkException
	 */
	@Action(value="updatePassWord",results={ @Result(type = "json", name = "success", params = {
			"root", "jsonObject" }) })
	public String updatePassWord() throws UnsupportedEncodingException, FrameworkException{
		// 当前登录用户
		SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext
		.getContext().getSession().get(CommonConstant.SESSION_USER);
		SysUser loginUser = null;
		try {
			loginUser = this.sysUserService.findSysUserById(sysUserLoginBean.getId());
		} catch (FrameworkException e) {
			e.printStackTrace();
		}
		try {
			BaseUtil.decodeObject(userPassWordBean);
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		Map<String , Object> jsonmMap = new HashMap<String, Object>();
		String error = "";

		String psw1 = userPassWordBean.getPassword1();//原始密码
		String psw2 = userPassWordBean.getPassword2();
		String psw3 = userPassWordBean.getPassword3();
		if(!psw2.equals(psw3)){
			jsonmMap.put("flag", false);
			error = "新密码和确认密码不一致!";
			return SUCCESS;
		}
		boolean flag = false;
		Map<String, SysUser> loginStatus = this.sysUserService.login(loginUser.getUserName(), MD5.getVsMd5(psw1));
		if(!BaseUtil.isEmpty(loginStatus)){
			SysUser sysuser = sysUserService.findByUserName(loginUser.getUserName());//获取登录用户实体			
			sysuser.setPassword(MD5.getVsMd5(psw3));
			sysUserService.modifySysUser(sysuser);
			flag = true;
		}else{
			error = "原始密码不正确";
		}
		jsonmMap.put("flag", flag);
		jsonmMap.put("error", error);
		jsonObject = JSONObject.fromObject(jsonmMap);
		return SUCCESS;
		
	}
	
	/*
	 * 获取登录者个人信息
	 */
	@Action(value="showUserList",results={ @Result(type = "json", name = "success", params = {
			"root", "jsonObject" }) })
	public String showUserList(){
		// 当前登录用户
		SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext
		.getContext().getSession().get(CommonConstant.SESSION_USER);
		SysUser loginUser = null;
		try {
			loginUser = this.sysUserService.findSysUserById(sysUserLoginBean.getId());
		} catch (FrameworkException e) {
			e.printStackTrace();
		}
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String username = loginUser.getRealName();
		String tel = loginUser.getMobile();
		String address = loginUser.getAddress();
		String email = loginUser.getEmail();
		jsonMap.put("username", username==null?"":username);
		jsonMap.put("tel", tel==null?"":tel);
		jsonMap.put("address", address==null?"":address);
		jsonMap.put("email", email==null?"":email);
		jsonObject = JSONObject.fromObject(jsonMap);
		return SUCCESS;
		
	}
	
	/**
	 * 编辑（修改）个人信息
	 * @return
	 */
	@Action(value="updateUserList",results={ @Result(type = "json", name = "success", params = {
			"root", "jsonObject" }) })
	public String updateUserList(){
		// 当前登录用户
		SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext
		.getContext().getSession().get(CommonConstant.SESSION_USER);
		SysUser loginUser = null;
		try {
			loginUser = this.sysUserService.findSysUserById(sysUserLoginBean.getId());
		} catch (FrameworkException e) {
			e.printStackTrace();
		}
		try {
			BaseUtil.decodeObject(userPassWordBean);
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		}
		Map<String , Object> jsonmMap = new HashMap<String, Object>();
		loginUser.setRealName(userPassWordBean.getUsername());
		loginUser.setMobile(userPassWordBean.getTel());
		loginUser.setAddress(userPassWordBean.getAddress());
		loginUser.setEmail(userPassWordBean.getEmail());
		boolean flag = false;
			if(!BaseUtil.isEmpty(loginUser)){
				try {
					sysUserService.modifySysUser(loginUser);
					flag = true;
				} catch (FrameworkException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		jsonmMap.put("flag", flag);
		jsonObject = JSONObject.fromObject(jsonmMap);
		return SUCCESS;
		
	}
}
